<template>
  <view class="goods-detail-container" v-if="goodsDetailData">
    <swiper indicator-dots autoplay circular :interval="3000">
      <swiper-item v-for="item in goodsDetailData.pics" :key="item.pics_id">
        <image
          :src="item.pics_big"
          mode="scaleToFill"
          @click="preview(item.pics_big)"
        />
      </swiper-item>
    </swiper>

    <!-- 商品信息区域 -->
    <view class="goods-info-box">
      <!-- 商品价格 -->
      <view class="price">￥{{ goodsDetailData.goods_price }}</view>
      <!-- 信息主体区域 -->
      <view class="goods-info-body">
        <!-- 商品名称 -->
        <view class="goods-name">{{ goodsDetailData.goods_name }}</view>
        <!-- 收藏 -->
        <view class="favi">
          <uni-icons type="star" size="18" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <!-- 运费 -->
      <view class="yf">快递：免运费</view>
    </view>

    <!-- <view v-html="goodsDetailData.goods_introduce"></view> -->
    <rich-text :nodes="goodsDetailData.goods_introduce"></rich-text>

    <!-- 商品导航 -->
    <uni-goods-nav
      class="goods_nav"
      :fill="true"
      :options="options"
      :buttonGroup="buttonGroup"
      @click="onClick"
      @buttonClick="buttonClick"
    />
  </view>
</template>

<script>
import { mapGetters } from 'vuex'
import { getGoodsDetailApi } from '@/api/index'
export default {
  computed: {
    ...mapGetters('cart', ['goodsInfo'])
  },
  data () {
    return {
      goodsDetailData: '',
      options: [{
        icon: 'shop',
        text: '店铺',
      }, {
        icon: 'cart',
        text: '购物车',
        info: 0
      }],
      buttonGroup: [{
        text: '加入购物车',
        backgroundColor: '#ff0000',
        color: '#fff'
      },
      {
        text: '立即购买',
        backgroundColor: '#ffa200',
        color: '#fff'
      }
      ]
    };
  },

  onLoad (options) {
    this.getGoodsDetail(options.goods_id)
    if (this.goodsInfo.length > 0) {
      this.options[1].info = this.goodsInfo.length
    }
  },


  methods: {
    onClick (e) {
      if (e.content.text === '购物车') {
        uni.switchTab({
          url: '/pages/cart/index'
        })
      }
    },
    buttonClick (e) {
      if (e.content.text === '加入购物车') {
        const find = this.goodsInfo.find(item => item.goods_id === this.goodsDetailData.goods_id)
        if (!find) {
          this.options[1].info++
        }
        // 准备商品对象，存入vuex中
        const goods = {
          goods_id: this.goodsDetailData.goods_id,
          goods_name: this.goodsDetailData.goods_name,
          goods_price: this.goodsDetailData.goods_price,
          goods_small_logo: this.goodsDetailData.goods_small_logo,
          goods_number: 1,
          goods_state: true // 状态，默认选中
        }
        uni.showToast({
          title: '加入购物车成功',
          icon: 'success',
          mask: true
        })
        this.$store.commit('cart/setGoodsInfo', goods)
      }
    },

    preview (current) {
      uni.previewImage({
        current, //当前预览那张图片
        urls: this.goodsDetailData.pics.map(item => item.pics_big),
      })
    },

    async getGoodsDetail (id) {
      const { meta: { status }, message } = await getGoodsDetailApi({ goods_id: id })

      if (status === 200) {
        message.goods_introduce = message.goods_introduce.replace(
          /<img/g,
          '<img style="display:block"'
        ).replace(
          /webp/g,
          'jpg'
        )
        this.goodsDetailData = message
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.goods-detail-container {
  // 给页面外层的容器，添加 50px 的内padding，
  // 防止页面内容被底部的商品导航组件遮盖
  padding-bottom: 50px;
  swiper {
    height: 750rpx;

    image {
      width: 100%;
      height: 100%;
    }
  }
  // 商品信息区域的样式
  .goods-info-box {
    padding: 10px;
    padding-right: 0;

    .price {
      color: #c00000;
      font-size: 18px;
      margin: 10px 0;
    }

    .goods-info-body {
      display: flex;
      justify-content: space-between;

      .goods-name {
        font-size: 13px;
        padding-right: 10px;
      }
      // 收藏区域
      .favi {
        width: 120px;
        font-size: 12px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        border-left: 1px solid #efefef;
        color: gray;
      }
    }

    // 运费
    .yf {
      margin: 10px 0;
      font-size: 12px;
      color: gray;
    }
  }
  .goods_nav {
    // 为商品导航组件添加固定定位
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
  }
}
</style>